<template>
  <div>
    <a-card :bordered="false" class="ant-pro-components-tag-select" v-show="!hiddenSearch">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="28">
            <a-col :md="5" :sm="12">
              <a-form-item label="道路名称">
                <a-input v-model="queryParam.roadName" placeholder="请输入道路名称"/>
              </a-form-item>
            </a-col>
            <a-col :md="5" :sm="12">
              <a-form-item label="道路代码">
                <a-input v-model="queryParam.roadCode" placeholder="请输入道路代码"/>
              </a-form-item>
            </a-col>
            <a-col :md="1" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-tooltip placement="topLeft" title="查询" arrow-point-at-center>
                  <a-button shape="circle" icon="search" @click="$refs.table.refresh(true)"/>
                </a-tooltip>
                <a-tooltip placement="topLeft" title="重置" arrow-point-at-center>
                  <a-button style="margin-left: 8px" @click="() => this.queryParam = {}" icon="sync"/>
                </a-tooltip>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card
      style="margin-top: 10px"
      :bordered="false">
      <div class="table-operator">
        <a-tooltip placement="topLeft" title="隐藏/显示搜索栏" arrow-point-at-center>
          <a-button
            size="small"
            :style="{position:'absolute',right:'55px',top: addRoadEnable && removeRoadEnable? '25px':'6px'}"
            shape="circle"
            :icon="icon"
            @click="hiddenSearchMenu(hiddenSearch)"/>
        </a-tooltip>
        <a-button v-show="addRoadEnable" type="primary" shape="round" icon="plus-circle" @click="$refs.modal.add()">新增</a-button>
        <a-button v-show="removeRoadEnable && selectedRowKeys.length > 0" type="danger" shape="round" icon="delete" @click="onClick(selectedRowKeys)">删除</a-button>
      </div>
      <s-table
        :scroll="{ x: 1500 }"
        ref="table"
        row-key="id"
        size="middle"
        :columns="columns"
        :data="loadData"
        :rowSelection="removeRoadEnable ? {selectedRowKeys: selectedRowKeys, onChange: onSelectChange} : null"
        showPagination="auto"
        :showTotal="true">
        <span slot="roadCode" slot-scope="text, record">
          {{ record.areaId + record.roadNo + record.crossingNo }}
        </span>
        <span slot="action" slot-scope="text, record">
          <template>
            <a-dropdown :trigger="['click']">
              <a-menu slot="overlay" >
                <a-menu-item v-show="editRoadEnable"><a @click="handleEdit(record)"><a-icon type="edit" theme="filled"/> 编辑</a></a-menu-item>
              </a-menu>
              <a-button type="dashed" icon="bars"/>
            </a-dropdown>
          </template>
        </span>
      </s-table>
      <road-modal ref="modal" @ok="handleOk"/>
    </a-card>
  </div>
</template>

<script>
import { checkPermission } from '@/utils/permissions'
import {
  batchDelRoad,
  findRoadPage
} from '@/api/system'
import { STable } from '@/components'
import message from 'ant-design-vue/es/message'
import RoadModal from '../modules/RoadModal'
export default {
  components: {
    STable,
    RoadModal
  },
  name: 'Road',
  data () {
    return {
      // 查询参数
      queryParam: {},
      icon: 'eye-invisible',
      hiddenSearch: false,
      removeRoadEnable: checkPermission('setting:road:remove'),
      addRoadEnable: checkPermission('setting:road:add'),
      editRoadEnable: checkPermission('setting:road:edit'),
      selectedRowKeys: [],
      selectedRows: [],
      dictDataPlateType: [],
      dictDataPlateColor: [],
      // 表头
      columns: [
        {
          title: '道路名称',
          dataIndex: 'roadName',
          align: 'left',
          width: 100,
          fixed: 'left',
          ellipsis: true
        },
        {
          title: '行政区划',
          dataIndex: 'areasName',
          align: 'center',
          width: 100
        },
        {
          title: '道路编码',
          dataIndex: 'roadNo',
          align: 'center',
          width: 80
        },
        {
          title: '路口编码',
          dataIndex: 'crossingNo',
          align: 'center',
          width: 80
        },
        {
          title: '地点米数',
          dataIndex: 'locationMeters',
          align: 'center',
          width: 100
        },
        {
          title: '道路代码',
          dataIndex: 'roadCode',
          align: 'center',
          width: 100,
          scopedSlots: { customRender: 'roadCode' }
        },
        {
          title: '操作',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' },
          align: 'center',
          width: 100,
          fixed: 'right'
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return findRoadPage(Object.assign(parameter, this.queryParam))
          .then(res => {
            return res.data
          })
      }
    }
  },
  created () {
  },
  methods: {
    handleEdit (record) {
      this.$refs.modal.edit(record)
    },
    handleOk () {
      this.$refs.table.refresh()
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    hiddenSearchMenu (hiddenSearch) {
      this.hiddenSearch = hiddenSearch !== true
      this.icon = this.hiddenSearch === true ? 'eye' : 'eye-invisible'
    },
    onClick (ids) {
      const self = this
      this.$confirm({
        title: '你确定删除吗?',
        content: <div style="color:red;">删除将永久不能恢复</div>,
        onOk () {
          batchDelRoad(ids).then(() => {
            self.$refs.table.refresh()
            message.success('操作成功')
            self.selectedRowKeys = []
          })
        },
        onCancel () {
          self.selectedRowKeys = []
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
  /deep/.ant-form-item-control {
    position: relative;
    line-height: 30px;
    zoom: 1;
  }
</style>
